<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Link_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    public function index() {
    	$sql = "SELECT * FROM uac_link WHERE state > 0 ORDER BY sort_order DESC";
        $flag = $this->db->query($sql);
        return $flag->result_array();
    }
    
	public function linkList($type = 'new', $num = 20) {
		if ($type == 'new') {
			$sql = "SELECT * FROM uac_link ORDER BY id DESC LIMIT $num";
		} else {
			$sql = "SELECT * FROM uac_link WHERE state > 0 ORDER BY sort_order DESC";
		}
        $flag = $this->db->query($sql);
        return $flag->result_array();
    }
	
	public function add($sitename = '', $url = '')
	{
		if ($sitename && $url) {
			$url = prep_url($url);
			$sql = "SELECT * FROM uac_link WHERE url = '" . $url . "' LIMIT 1";
        	$flag = $this->db->query($sql);
        	if (!$flag->row()) {
        		$data = array(
	               'name' => "$sitename" ,
	               'url' => "$url" ,
	            );
	            $flag = $this->db->insert('uac_link', $data);
	            if ($flag) { return TRUE; }
        	}
		}
		return FALSE;
	}
}